$(document).ready(function(){
	
	$('#team_change').click(function(){
		$(".modal").modal('show');
	})
	$('.team_change_ok').click(function(){
		$('.modal').modal('hide');
	})
	
	  $(".header").load("header.html"); 
	  $(".footer").load("footer.html");
	  
	  $(".form-horizontal").submit(function(){
		  if(confirm("정말 신청하시겠습니까?")){
			  alert("신청되었습니다.");
			  return true;
		  }
		  return false;
		  
	  })
	
});


  
  var myModule = angular.module('myModule', []);
  

	
	
  myModule.controller('MyController', function ($scope,$http,$location) {
      
	  
	  
      $scope.contest_reg={};
      $scope.track=[];
      $scope.contest_reg_id=$location.search().contest_reg_id;
      $scope.user={};
      $scope.track_id=null;
      $scope.str=null;
      $scope.temp='name';
      
      $scope.current=[];
      $scope.search=[];
      $scope.select=[];
      $scope.trackMessage={};
      $scope.teamNameMessage={};
      
      $scope.total=[];
      $scope.teamName=null;
      
      
      $scope.loadData = function() {
      	myUtil.onHttp($http,'get','contest_reg.do',{id : $scope.contest_reg_id},null,
      			function(data){
      				$scope.contest_reg=data.contest_reg;
      				$scope.track=data.track;
      				$scope.user=data.user;
      				$scope.track_id=$scope.track[0].track_id;
      				$scope.leader=$scope.user.id;
      				$scope.change();
      			}
      	)
      }
      
      $scope.change=function(){
    	  $scope.total=$scope.current.slice(0);
    	  $scope.total.push($scope.user);

    	  myUtil.onHttp($http,'post','trackCheck.do',{track_id : $scope.track_id, contest_reg_id : $scope.contest_reg_id} ,$scope.total,
    			  function(data){
    		  	  		$scope.trackMessage=data;
    	  		  }
    	  )
      }

      
      $scope.teamChange=function(){
    	  myUtil.onHttp($http,'get','teamChange.do',{contest_reg_id : $scope.contest_reg_id,track_id: $scope.track_id,str:$scope.str,temp:$scope.temp},null,
        			function(data){
    		  			$scope.search=data;
        			}
        	)
      }
      
      $scope.teamNameCheck=function(){
    	  myUtil.onHttp($http,'get','teamNameCheck.do',{contest_reg_id : $scope.contest_reg_id, teamName:$scope.teamName},null,
      			function(data){
  		  			$scope.teamNameMessage=data;
      			}
      	)
      }
      $scope.disabled=function(){
    	  return ($scope.trackMessage.check && $scope.teamNameMessage.check);
      }

      $scope.addSelect=function(index){
    	  $scope.select.push($scope.search[index]);
      }
      
      $scope.removeSelect=function(index){
    	  $scope.select.splice(index,1);
      }
      
      $scope.checkSelect=function(index){
    	  var result=false;
    	  for(var i=0;i<$scope.select.length && !result;i++)
    		  if($scope.select[i].id==$scope.search[index].id)
    			  result=true;
    	  return result;
      }
      $scope.init=function(){
    	  $scope.select=$scope.current;
    	  $scope.closeModal();
      }
      
      
      $scope.addCurrent=function(){
    	  $scope.closeModal();
    	  $scope.current=$scope.select;
    	  
      }
      $scope.closeModal=function(){
    	  $scope.search=[];
    	  $scope.temp='name';
    	  $scope.str=null;
      }
         
      
      
      $scope.loadData();
      
})
